package com.controller.student;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.Record;
import com.po.StudentChoose;
import com.po.StudentComment;
import com.po.TodayMenu;
import com.po.TomorrowMenu;
import com.service.student.MenuService;

@Controller
public class MenuController {
	@Resource(name="menuServiceImp")
	private MenuService menuService;
	//加载今日菜单
	@ResponseBody
	@RequestMapping(value="queryTodayMenu")
	public List<TodayMenu> queryTodayMenuList(@RequestParam("date") String date,HttpServletRequest request){
		String path = request.getServletContext().getRealPath("/student/dish/");
		List<TodayMenu>	 list = menuService.queryTodayMenuList(date);
		List<TodayMenu> list1 = getList(list, path);
		return list1;
	}
	//更新今日菜单评价
	@ResponseBody
	@RequestMapping(value="addComment")
	public List<TodayMenu> addStuComment(@RequestBody List<StudentComment> stuCommentList, HttpServletRequest request){
		String date = stuCommentList.get(0).getComment_date();
		String stu_id = stuCommentList.get(0).getStudent_id();
		String date1 = date.substring(0, 10);
		//记录该ID评价过今日菜单
		Record record = new Record();
		record.setComment_state(1);
		record.setStudent_id(stu_id);
		record.setTime(date1);
		menuService.changeRecord(record);
		
		//向评论表添加数据
		if(!stuCommentList.isEmpty()){
			for(StudentComment comment: stuCommentList){
				menuService.addStudentComment(comment);
			}	
		}
		
		String path = request.getServletContext().getRealPath("/student/dish/");
		List<TodayMenu> list = menuService.getTodayMenuList(date1);	
		List<TodayMenu> list1 = getList(list, path);
		return list1;
	}
	
	//查询带评价的今日菜单
	@ResponseBody
	@RequestMapping(value="updateTodayMenu")
	public List<TodayMenu> getTodayMenuList(@RequestParam("date") String date,  HttpServletRequest request ){
		String path = request.getServletContext().getRealPath("/student/dish/");
		List<TodayMenu> list = menuService.getTodayMenuList(date);	
		List<TodayMenu> list1 = getList(list, path);
		return list1;
	}
	
	//查询带选餐人数的明日菜单
	@ResponseBody
	@RequestMapping(value="queryTomorrowMenu")
	public List<TomorrowMenu> getTomorrowMenuList(@RequestParam("date") String date,  HttpServletRequest request){
		String path = request.getServletContext().getRealPath("/student/dish/");
		List<TomorrowMenu> list =  menuService.getTomorrowMenuLsit(date);
		List<TomorrowMenu> list1 = getList1(list, path);
		 return list1;

	}
	
	//更新明日菜单的选餐人数
	@ResponseBody
	@RequestMapping(value="updateTomorrowMenu")
	public List<TomorrowMenu> updateTomorrowMenuList(@RequestBody List<StudentChoose> TomorrowList,  HttpServletRequest request){
		String path = request.getServletContext().getRealPath("/student/dish/");
		String date = TomorrowList.get(0).getDate();
		String stu_id = TomorrowList.get(0).getStudent_id();
		//记录该ID已经选择过明日菜单
		Record record = new Record();
		record.setStudent_id(stu_id);
		record.setTime(date);
		record.setChoose_state(1);
		menuService.changeRecord(record);
		
		//更新明日菜单人数
		if(!TomorrowList.isEmpty()){
		for(StudentChoose item: TomorrowList){
			item.setDate(date);
			menuService.updateTomorrowMenuList(item);	
		 }
		}
		List<TomorrowMenu> list =  menuService.getTomorrowMenuLsit(date);
		List<TomorrowMenu> list1 = getList1(list, path);
		return list1;
	}
	
	//查询该ID是否评价或点餐过
	@ResponseBody
	@RequestMapping(value="queryRecord")
	public Record queryRecord(@RequestBody Record record){
		System.out.println(record.getStudent_id()+"..."+record.getTime());
		Record result = menuService.queryRecord(record);
		if(result != null){
			return result;
		}
		else{
			Record result1 = new Record();
			result1.setStudent_id(record.getStudent_id());
			result1.setChoose_state(0);
			result1.setComment_state(0);
			return result1;
		}
	}
	//获取带图片的今日菜单
	public List<TodayMenu> getList(List<TodayMenu> todayList, String path){
		for(TodayMenu menu: todayList){
			String imagePath = path+menu.getDish_image();
			File f = new File(imagePath);
			BufferedImage bi;
		    try {
		    	bi = ImageIO.read(f);
			    ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ImageIO.write(bi, "jpg", baos);
				byte[] bytes = baos.toByteArray();
				menu.setImage(bytes);
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
		}
		return todayList;	
	}
	//获取带图片的明日菜单
	public List<TomorrowMenu> getList1(List<TomorrowMenu>tomorrowList, String path){
		for(TomorrowMenu menu: tomorrowList){
			String imagePath = path+menu.getDish_image();
			File f = new File(imagePath);
			BufferedImage bi;
		    try {
		    	bi = ImageIO.read(f);
			    ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ImageIO.write(bi, "jpg", baos);
				byte[] bytes = baos.toByteArray();
				menu.setImage(bytes);
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
		}
		return tomorrowList;	
	}
}
